<?php
class CustomerIndex extends BaseDB
{
	function __construct()
	{
		$this->table = 'customer_index';
		parent::__construct();
	}
	
	/**
	 * 添加客户索引信息
	 *
	 * @param $data array('pid', 'name')
	 */
	public function add($data)
	{
		if (empty($data['name'])) {
			return false;
		}
		if (empty($data['pid']) || !is_numeric($data['pid'])) {
			return false;
		}
		$data['pid'] = max(0, $data['pid']);
		return $this->insert($data);
	}
	
	public function getInfoByName($name, $pid) 
	{
		if (empty($name) || $pid < 0) {
			return false;
		}
		
		$res = $this->select(array('name'=>$name, 'pid'=>$pid));
		if (!empty($res)) {
			return $res[0];
		}
		return false;
	}
	
	public function getInfoById($id)
	{
		if (empty($id))return false;
		$res = $this->select(array('id'=>$id));
		if (!empty($res)) {
			return $res[0];
		}
		return false;
	}
	
	public function getInfoByIds($ids)
	{
		if (empty($ids) || !is_array($ids))return false;
		return $this->getXByIds($ids);
	}
	
	public function getList($where = false, $start = 0 , $count = 20)
	{
		if (!is_array($where) || empty($where))return false;
		return array(
			'total' => $this->getCount($where), 
			'data' => $this->select($where, $start, $count)
		);
	}
	
	public function queryByName($name, $start = 0, $count = 1)
	{
		if (empty($name)) return false;
		$start = max(0, (int)$start);
		$count = max(0, (int)$count);
		$sql = "select * from " . $this->table . " where name like '%$name%' limit $start, $count order by id desc";
		return $this->db->getAll($sql, array());
	}
	
	public function modify($data, $where) {
		if (empty($where)) {
			return false;
		}
		
		return $this->update($data, $where);
	}
}
?>